package com.letv.android.client.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

import com.letv.android.young.client.R;

public class LetvSlipSwitch extends View implements OnTouchListener,OnClickListener {

	private Bitmap switch_on_bkg;
	private Bitmap switch_off_bkg;
	private Bitmap btn_slip;
	private Rect rect_on;
	private Rect rect_off;
	private boolean isSwitchOn;// 当前开关的状态
	private OnSlipSwitchListener switchListenerState;// 开关的监听器
	private boolean isSwitchListener;// 是否使用了开关监听
	private boolean isSlipping;//是否处于滑动状态
	private float preX;
	private float currentX;
	private boolean preSwitchState;//上一次开关的状态

	public LetvSlipSwitch(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public LetvSlipSwitch(Context context) {
		super(context);
		init();
	}

	private void init() {
//		setOnTouchListener(this);
		setOnClickListener(this);
		setImageRes(R.drawable.letv_slide_toggle_on, R.drawable.letv_slide_toggle_off, R.drawable.letv_slide_toggle);
	}
	@Override
	public void onClick(View v) {
		preSwitchState = isSwitchOn;
		isSlipping = false; 
		isSwitchOn = !isSwitchOn;
		//如果使用了开关监听器，开关的状态发生了改变，执行方法
		if(isSwitchListener && preSwitchState != isSwitchOn){
			switchListenerState.onSwitched(isSwitchOn);
		}
		
		invalidate();//重绘
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {

		switch (event.getAction()) {
		//按下
		case MotionEvent.ACTION_DOWN:
			isSlipping = true; 
			
			preX = event.getX();
			currentX = preX;
			
			break;

		//滑动
		case MotionEvent.ACTION_MOVE:
			
			currentX = event.getX();

			break;
		//松手
		case MotionEvent.ACTION_UP:
		default:
			preSwitchState = isSwitchOn;
			isSlipping = false;

			isSwitchOn = event.getX() > switch_on_bkg.getWidth() / 2;
			
			//如果使用了开关监听器，开关的状态发生了改变，执行方法
			if(isSwitchListener && preSwitchState != isSwitchOn){
				switchListenerState.onSwitched(isSwitchOn);
			}
			
			break;
		}
		
		invalidate();//重绘

		return true;
	}

	// 设置显示的图片
	public void setImageRes(int bkgSwitchOn, int bkgSwitchOff, int btnSlip) {
		// 开关状态为开启的背景图
		switch_on_bkg = BitmapFactory.decodeResource(getResources(),
				bkgSwitchOn);
		// 开关状态为关闭的背景图
		switch_off_bkg = BitmapFactory.decodeResource(getResources(),
				bkgSwitchOff);
		// 滑动开关图片
		btn_slip = BitmapFactory.decodeResource(getResources(), btnSlip);

		// 矩形 用来指定开关的位置的(开启)
		rect_on = new Rect(switch_on_bkg.getWidth() - btn_slip.getWidth(), 0,
				switch_on_bkg.getWidth(), btn_slip.getHeight());

		// 矩形 用来指定开关的位置的(关闭)
		rect_off = new Rect(0, 0, btn_slip.getWidth(), btn_slip.getHeight());
	}

	// 设置开关当前状态
	public void setSwitchState(boolean state) {
		isSwitchOn = state;
		invalidate();//重绘
	}

	// 设置开关监听
	public void setSlipSwitchListener(OnSlipSwitchListener listener) {
		switchListenerState = listener;
		isSwitchListener = true;
	}

	public interface OnSlipSwitchListener {

		void onSwitched(boolean state);
	}

	//测量、估量 开关的显示大小(尺寸)
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		
		setMeasuredDimension(switch_on_bkg.getWidth(), switch_on_bkg.getHeight());
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		//滑动开关的左边位置
		int left_slip = 0;
		
		Matrix matrix = new Matrix();
		Paint paint = new Paint();
		
//		if(currentX > switch_on_bkg.getWidth() /2 ){
//			//开关开启状态，设置背景图
//			canvas.drawBitmap(switch_on_bkg, matrix, paint);
//		} else {
//			//开关关闭状态，设置背景图
//			canvas.drawBitmap(switch_off_bkg, matrix, paint);
//		}
//		
//		if(isSlipping){
//			if(currentX > switch_on_bkg.getWidth()){
//				left_slip = switch_on_bkg.getWidth() - btn_slip.getWidth();
//			} else {
//				left_slip = (int) (currentX - btn_slip.getWidth() /2);
//			}
//		} else {
			if(isSwitchOn){
				//开关开启状态
				left_slip = rect_on.left;
				canvas.drawBitmap(switch_on_bkg, matrix, paint);
			} else {
				//开关关闭状态
				left_slip = rect_off.left;
				canvas.drawBitmap(switch_off_bkg, matrix, paint);
			}
//		}
		
		//判断边距
//		if(left_slip > switch_on_bkg.getWidth() - btn_slip.getWidth()){
//			left_slip = switch_on_bkg.getWidth() - btn_slip.getWidth();
//		} else if(left_slip < 0){
//			left_slip = 0;
//		}
		canvas.drawBitmap(btn_slip, left_slip, 0, paint);
		

	}
}
